package gghotline

import (
	"github.com/shopspring/decimal"
	"wzgames/internal/model/gkindp"
	"wzgames/internal/secure/rng"
)

const (
	hot   = "hot"
	red   = "red"
	black = "black"
)

type Gift struct {
	total      int32
	hotIdx1    int
	hotIdx2    int
	startColor int

	rails []int
}

func (gf *Gift) Init(cfg *gkindp.HotLineConfig) {
	gf.total = 90
	gf.hotIdx1 = 15
	gf.hotIdx2 = 75
	gf.startColor = 0 // 0-red, 1-black, 2-hot

	total := int(gf.total)
	for i := 0; i < total; i++ {
		if i == gf.hotIdx1 || i == gf.hotIdx2 {
			gf.rails = append(gf.rails, 2)
		} else {
			if gf.startColor == 0 {
				gf.rails = append(gf.rails, 0)
				gf.startColor = 1
			} else {
				gf.rails = append(gf.rails, 1)
				gf.startColor = 0
			}
		}
	}
}

func (gf *Gift) Update(cfg *gkindp.HotLineConfig) {

}

func (gf *Gift) Calculate(servSeed, clientSeed string, highRisk bool) []string {
	limit := 1
	if highRisk {
		limit = 2
	}
	nums := rng.CalculateHash(servSeed, clientSeed, rng.ChunkSize, limit)
	idx := decimal.NewFromFloat(nums[0]).Mul(decimal.NewFromInt32(gf.total))
	idx1 := int(idx.IntPart())
	var ret []string
	ret = append(ret, gf.getColor(idx1))
	if highRisk {
		idx = decimal.NewFromFloat(nums[1]).Mul(decimal.NewFromInt32(gf.total))
		idx2 := int(idx.IntPart())
		ret = append(ret, gf.getColor(idx2))
	}
	return ret
}

func (gf *Gift) getColor(idx int) string {
	switch gf.rails[idx] {
	case 0:
		return red
	case 1:
		return black
	case 2:
		return hot
	default:
		return black
	}
}
